package com.px.device.service.impl;

import com.px.common.constant.HttpStatus;
import com.px.common.exception.ServiceException;
import com.px.common.utils.DateUtils;
import com.px.common.utils.MinioUtil;
import com.px.common.utils.file.FileUtils;
import com.px.common.utils.uuid.Seq;
import com.px.device.domain.*;
import com.px.device.mapper.RoutesConfigMapper;
import com.px.device.mapper.RoutesInfoMapper;
import com.px.device.mapper.RoutesPointActionMapper;
import com.px.device.mapper.RoutesPointMapper;
import com.px.device.service.IRoutesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 航线基础信息Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class RoutesInfoServiceImpl implements IRoutesInfoService 
{
    @Autowired
    private RoutesInfoMapper routesInfoMapper;
    
    @Autowired
    private RoutesConfigMapper routesConfigMapper;

    @Autowired
    private RoutesPointMapper routesPointMapper;

    @Autowired
    private RoutesPointActionMapper routesPointActionMapper;

    @Value("${minio.bucketName}")
    private String bucketName;

    /**
     * 查询航线基础信息
     * 
     * @param routesId 航线基础信息主键
     * @return 航线基础信息
     */
    @Override
    public RoutesVo selectRoutesInfoByRoutesId(Long routesId)
    {
        RoutesVo routesVo = new RoutesVo();
        RoutesInfo routesInfo = routesInfoMapper.selectRoutesInfoByRoutesId(routesId);
        routesVo.setRoutesInfo(routesInfo);
        RoutesConfig routesConfig = routesConfigMapper.selectRoutesConfigByRoutesRoutesId(routesId);
        routesVo.setRoutesConfig(routesConfig);
        List<RoutesPoint> routesPointList = routesPointMapper.selectRoutesPointByRoutesConfigId(routesConfig.getRoutesConfigId());
        routesPointList.forEach(routesPoint ->{
            List<RoutesPointAction> RoutesPointActionList = routesPointActionMapper.selectRoutesPointActionByRoutesPointId(routesPoint.getRoutesPointId());
            routesPoint.setRoutesPointActionList(RoutesPointActionList);
        });
        routesVo.setRoutesPointList(routesPointList);
        return routesVo;
    }

    /**
     * 查询航线基础信息列表
     * 
     * @param routesInfo 航线基础信息
     * @return 航线基础信息
     */
    @Override
    public List<RoutesInfo> selectRoutesInfoList(RoutesInfo routesInfo)
    {
        return routesInfoMapper.selectRoutesInfoList(routesInfo);
    }

    /**
     * 新增航线基础信息
     * 
     * @param routesVo 航线基础信息
     * @return 结果
     */
    @Override
    public int insertRoutesInfo(RoutesVo routesVo) throws Exception {

        int routesNameCount = routesInfoMapper.getRoutesName(routesVo.routesInfo.getRoutesName());
        if (routesNameCount > 0){
            throw new ServiceException("航线名称重复", HttpStatus.BAD_REQUEST);
        }

        routesVo.routesInfo.setCreateTime(DateUtils.getNowDate());
        //转并获取kmz文件路径
        String routesPath = getRoutesPath(routesVo);

        routesVo.routesInfo.setRoutesPath(routesPath);
        int  routesId = routesInfoMapper.insertRoutesInfo(routesVo.routesInfo);
        routesVo.routesConfig.setRoutesId(routesVo.routesInfo.getRoutesId());
        routesConfigMapper.insertRoutesConfig(routesVo.routesConfig);
        routesVo.routesPointList.stream().forEach(routesPoint ->{
            routesPoint.setRoutesConfigId(routesVo.routesConfig.getRoutesConfigId());
            routesPointMapper.insertRoutesPoint(routesPoint);
            routesPoint.routesPointActionList.stream().forEach(routesPointAction ->{
                routesPointAction.setRoutesPointId(routesPoint.getRoutesPointId());
                routesPointActionMapper.insertRoutesPointAction(routesPointAction);
            });
        });

        return routesId;
    }

    private String getRoutesPath(RoutesVo routesVo) throws Exception {
        WpmlConversion wpmlConversion = new WpmlConversion();
        routesVo.routesInfo.setUpdateTime(DateUtils.getNowDate());
        Long droneEnumValue = routesVo.routesConfig.getDroneEnumValue();
        Long droneSubEnumValue = routesVo.routesConfig.getDroneSubEnumValue();
        if(droneEnumValue == 67){
            if(droneSubEnumValue == 0){
                routesVo.routesConfig.setPayloadEnumValue(52L);
            }else {
                routesVo.routesConfig.setPayloadEnumValue(53L);
            }
        }else {
            if(droneSubEnumValue == 0){
                routesVo.routesConfig.setPayloadEnumValue(66L);
            }else if(droneSubEnumValue == 1){
                routesVo.routesConfig.setPayloadEnumValue(67L);
            }else {
                routesVo.routesConfig.setPayloadEnumValue(68L);
            }
        }
        //转文件存服务器
        FileUtils.writeByKml(wpmlConversion.getkml(routesVo),routesVo.routesInfo.getRoutesName());
        FileUtils.writeByWpml(wpmlConversion.getwpml(routesVo),routesVo.routesInfo.getRoutesName());
        String routesPath = FileUtils.toZip(routesVo.routesInfo.getRoutesName());
        //文件转存Minio
        String objectName = "kmz/"+routesVo.routesInfo.getRoutesName()+"/"+routesVo.routesInfo.getRoutesName()+ "_" +Seq.getId(Seq.uploadSeqType)+".kmz";
        MinioUtil.uploadFile(bucketName,objectName,routesPath);
        return bucketName + "/" + objectName;
    }

    /**
     * 修改航线基础信息
     * 
     * @param routesVo 航线基础信息
     * @return 结果
     */
    @Override
    public int updateRoutesInfo(RoutesVo routesVo) throws Exception {
        int routesNameCount = routesInfoMapper.getRoutesNameById(routesVo.routesInfo);
        if (routesNameCount > 0){
            throw new ServiceException("航线名称重复", HttpStatus.BAD_REQUEST);
        }
        //转并获取kmz文件路径
        String routesPath = getRoutesPath(routesVo);

        routesVo.routesInfo.setRoutesPath(routesPath);
        int  routesId = routesInfoMapper.updateRoutesInfo(routesVo.routesInfo);
        routesVo.routesConfig.setRoutesId(routesVo.routesInfo.getRoutesId());
        routesConfigMapper.updateRoutesConfig(routesVo.routesConfig);
        routesPointMapper.deleteRoutesPointByRoutesConfigId(routesVo.routesConfig.getRoutesConfigId());
        routesVo.routesPointList.stream().forEach(routesPoint ->{
            routesPoint.setRoutesConfigId(routesVo.routesConfig.getRoutesConfigId());
            routesPointMapper.insertRoutesPoint(routesPoint);
            routesPointActionMapper.deleteRoutesPointActionByRoutesPointId(routesPoint.getRoutesPointId());
            routesPoint.routesPointActionList.stream().forEach(routesPointAction ->{

                routesPointAction.setRoutesPointId(routesPoint.getRoutesPointId());
                routesPointActionMapper.insertRoutesPointAction(routesPointAction);
            });
        });

        return routesId;
    }

    /**
     * 批量删除航线基础信息
     * 
     * @param routesIds 需要删除的航线基础信息主键
     * @return 结果
     */
    @Override
    public int deleteRoutesInfoByRoutesIds(Long[] routesIds)
    {
        return routesInfoMapper.deleteRoutesInfoByRoutesIds(routesIds);
    }

    /**
     * 删除航线基础信息信息
     * 
     * @param routesId 航线基础信息主键
     * @return 结果
     */
    @Override
    public int deleteRoutesInfoByRoutesId(Long routesId)
    {
        return routesInfoMapper.deleteRoutesInfoByRoutesId(routesId);
    }

    @Override
    public List<RoutesInfo> selectRoutesInfoByRoutesIds(Long[] wayPointIds) {
        return routesInfoMapper.selectRoutesInfoByRoutesIds(wayPointIds);
    }

    @Override
    public List<RoutesInfo> selectRoutesInfoListNotAirport(RoutesInfo routesInfo) {
        return routesInfoMapper.selectRoutesInfoListNotAirport(routesInfo);
    }

    @Override
    public List<RoutesInfo> getRoutesListByUserId(Long userId) {
        return routesInfoMapper.getRoutesListByUserId(userId);
    }

}
